package com.sth.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sth.commonutils.R;
import com.sth.commonutils.UUIDUtil;
import com.sth.commonutils.ordervo.CourseWebVoOrder;
import com.sth.commonutils.ordervo.UcenterMemberOrder;
import com.sth.order.client.EduClient;
import com.sth.order.client.UcenterClient;
import com.sth.order.client.Uncenter;
import com.sth.order.entity.Order;
import com.sth.order.mapper.OrderMapper;
import com.sth.order.service.OrderService;
import com.sth.order.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author sth
 * @since 2020-12-20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private Uncenter uncenter;
    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    // 1. 生成订单
    @Override
    public String saveOrder(String courseId, String memberId) {
        // 1.通过远程调用根据用户id获取用户信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);
        // 2. 通过远程调用根据课程id获取课程信息
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);
        Order order = new Order();
        order.setId(UUIDUtil.get8UUID());
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName("test");
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberId);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());
        order.setStatus(0);// 订单状态 0 未支付 1 已支付
        order.setPayType(1);// 支付类型 微信1
        baseMapper.insert(order);
        // 返回订单号
        return order.getOrderNo();
    }

    @Override
    public void test(String courseId) {
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);
        System.out.println("---------------------");
        System.out.println(courseInfoOrder);
    }

    @Override
    public void registerCount(String day) {
        R r = uncenter.countRegister(day);
        System.out.println("-------------------------");
        System.out.println(r);
    }
}
